package com.yanqu.road.dao.impl.activity.shuihuyezicard;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardBuffConfig;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardCardConfig;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardLevelConfig;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardRoomConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/8 15:21
 */
public class ShuiHuYeZiCardConfigDaoImpl extends TempDao {


    public static String getShuiHuYeZiCardCardConfigSql = "select * from `t_s_activity_yezicard_card` where `activityId`=?";
    public Map<Integer,ShuiHuYeZiCardCardConfig> getShuiHuYeZiCardCardConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,ShuiHuYeZiCardCardConfig> map = new ConcurrentHashMap();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuiHuYeZiCardCardConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuiHuYeZiCardCardConfig cardConfig = parseShuiHuYeZiCardCardConfig(rs);
                map.put(cardConfig.getId(),cardConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getShuiHuYeZiCardBuffConfigSql = "select * from `t_s_activity_yezicard_buff` where `activityId`=?";
    public Map<Integer,ShuiHuYeZiCardBuffConfig> getShuiHuYeZiCardBuffConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,ShuiHuYeZiCardBuffConfig> map = new ConcurrentHashMap();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuiHuYeZiCardBuffConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuiHuYeZiCardBuffConfig buffConfig = parseShuiHuYeZiCardBuffConfig(rs);
                map.put(buffConfig.getId(),buffConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getShuiHuYeZiCardLevelConfigSql = "select * from `t_s_activity_yezicard_level` where `activityId`=?";
    public Map<Integer,ShuiHuYeZiCardLevelConfig> getShuiHuYeZiCardLevelConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,ShuiHuYeZiCardLevelConfig> map = new ConcurrentHashMap();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuiHuYeZiCardLevelConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuiHuYeZiCardLevelConfig levelConfig = parseShuiHuYeZiCardLevelConfig(rs);
                map.put(levelConfig.getId(),levelConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getShuiHuYeZiCardRoomConfigSql = "select * from `t_s_activity_yezicard_room` where `activityId`=?";
    public Map<Integer,ShuiHuYeZiCardRoomConfig> getShuiHuYeZiCardRoomConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,ShuiHuYeZiCardRoomConfig> map = new ConcurrentHashMap();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuiHuYeZiCardRoomConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuiHuYeZiCardRoomConfig roomConfig = parseShuiHuYeZiCardRoomConfig(rs);
                map.put(roomConfig.getId(),roomConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    protected ShuiHuYeZiCardCardConfig parseShuiHuYeZiCardCardConfig(ResultSet rs) throws SQLException {
        ShuiHuYeZiCardCardConfig shuiHuYeZiCardCardConfig = new ShuiHuYeZiCardCardConfig();
        shuiHuYeZiCardCardConfig.setActivityId(rs.getInt("activityId"));
        shuiHuYeZiCardCardConfig.setId(rs.getInt("id"));
        shuiHuYeZiCardCardConfig.setType(rs.getInt("type"));
        shuiHuYeZiCardCardConfig.setPoint(rs.getInt("point"));
        shuiHuYeZiCardCardConfig.setUpgradeCondition(rs.getString("upgradeCondition"));
        shuiHuYeZiCardCardConfig.setUpgradePoint(rs.getString("upgradePoint"));
        shuiHuYeZiCardCardConfig.setScore(rs.getInt("score"));
        shuiHuYeZiCardCardConfig.setGoldWeight(rs.getInt("goldWeight"));
        shuiHuYeZiCardCardConfig.setWeight(rs.getInt("weight"));

        shuiHuYeZiCardCardConfig.upgradeConditionList = StringUtils.stringToIntegerList(shuiHuYeZiCardCardConfig.getUpgradeCondition(),";");
        List<Integer> upgradePointList = StringUtils.stringToIntegerList(shuiHuYeZiCardCardConfig.getUpgradePoint(),";");

        int point = 0;
        for (int i = 0; i < upgradePointList.size(); i++) {
            point += upgradePointList.get(i);
            shuiHuYeZiCardCardConfig.levelPointMap.put(i+1,point);
        }
        return shuiHuYeZiCardCardConfig;
    }


    protected ShuiHuYeZiCardBuffConfig parseShuiHuYeZiCardBuffConfig(ResultSet rs) throws SQLException {
        ShuiHuYeZiCardBuffConfig shuiHuYeZiCardBuffConfig = new ShuiHuYeZiCardBuffConfig();
        shuiHuYeZiCardBuffConfig.setActivityId(rs.getInt("activityId"));
        shuiHuYeZiCardBuffConfig.setId(rs.getInt("id"));
        shuiHuYeZiCardBuffConfig.setType(rs.getInt("type"));
        shuiHuYeZiCardBuffConfig.setPar(rs.getString("par"));
        shuiHuYeZiCardBuffConfig.setRound(rs.getString("round"));
        shuiHuYeZiCardBuffConfig.setCondition(rs.getString("condition"));
        shuiHuYeZiCardBuffConfig.setWeight(rs.getInt("weight"));

        shuiHuYeZiCardBuffConfig.parList = StringUtils.stringToIntegerList(shuiHuYeZiCardBuffConfig.getPar(),"\\|");
        shuiHuYeZiCardBuffConfig.conditionList = StringUtils.stringToIntegerList(shuiHuYeZiCardBuffConfig.getCondition(),";");

        List<String> roundParList = StringUtils.stringToStringList(shuiHuYeZiCardBuffConfig.getRound(),"\\|");
        for (String item : roundParList){
            List<Integer> list = StringUtils.stringToIntegerList(item,";");
            shuiHuYeZiCardBuffConfig.roundMap.put(list.get(0),list.get(1));
        }

        return shuiHuYeZiCardBuffConfig;
    }

    protected ShuiHuYeZiCardLevelConfig parseShuiHuYeZiCardLevelConfig(ResultSet rs) throws SQLException {
        ShuiHuYeZiCardLevelConfig shuiHuYeZiCardLevelConfig = new ShuiHuYeZiCardLevelConfig();
        shuiHuYeZiCardLevelConfig.setActivityId(rs.getInt("activityId"));
        shuiHuYeZiCardLevelConfig.setId(rs.getInt("id"));
        shuiHuYeZiCardLevelConfig.setScore(rs.getInt("score"));
        shuiHuYeZiCardLevelConfig.setSucceedScore(rs.getInt("succeedScore"));
        shuiHuYeZiCardLevelConfig.setDefeatScore(rs.getInt("defeatScore"));
        shuiHuYeZiCardLevelConfig.setSucceedRate(rs.getInt("succeedRate"));
        shuiHuYeZiCardLevelConfig.setAccuracyAI(rs.getInt("accuracyAI"));
        shuiHuYeZiCardLevelConfig.setStarAI(rs.getInt("starAI"));
        shuiHuYeZiCardLevelConfig.setGoldAI(rs.getInt("goldAI"));

        return shuiHuYeZiCardLevelConfig;
    }

    protected ShuiHuYeZiCardRoomConfig parseShuiHuYeZiCardRoomConfig(ResultSet rs) throws SQLException {
        ShuiHuYeZiCardRoomConfig shuiHuYeZiCardRoomConfig = new ShuiHuYeZiCardRoomConfig();
        shuiHuYeZiCardRoomConfig.setActivityId(rs.getInt("activityId"));
        shuiHuYeZiCardRoomConfig.setId(rs.getInt("id"));
        shuiHuYeZiCardRoomConfig.setType(rs.getInt("type"));
        shuiHuYeZiCardRoomConfig.setConsume(rs.getString("consume"));
        shuiHuYeZiCardRoomConfig.setSucceedReward(rs.getString("succeedReward"));
        shuiHuYeZiCardRoomConfig.setDefeatReward(rs.getString("defeatReward"));
        shuiHuYeZiCardRoomConfig.setFreePlay(rs.getInt("freePlay"));
        shuiHuYeZiCardRoomConfig.setCondition(rs.getString("condition"));
        shuiHuYeZiCardRoomConfig.setTicketsCondition(rs.getInt("ticketsCondition"));
        shuiHuYeZiCardRoomConfig.setScoreParam(rs.getInt("scoreParam"));

        shuiHuYeZiCardRoomConfig.conditionList = StringUtils.stringToIntegerList(shuiHuYeZiCardRoomConfig.getCondition(),";");

        return shuiHuYeZiCardRoomConfig;
    }
}
